--author:lee
--date:2021.06.25

local ngx      = ngx
local table    = table
local tonumber = tonumber
local require  = require
local type = type
local ipairs = ipairs
local pairs = pairs
local cjson    = require("cjson.safe")

local log = require("modules.commhm.log")
local uu  = require("modules.commhm.utils")

local gg           = require("modules.business_welfare.welfare_gg")
local welfare_code = require("modules.business_welfare.welfare_code")
local lua_config   = require("modules.business_welfare.welfare_lua_config")
local auth_checker = require("modules.business_welfare.welfare_auth_checker")
local acts_func    = require("modules.business_welfare.welfare_acts")
local cmds_func    = require("modules.business_welfare.welfare_cmds")
local op_log     = require("modules.business_welfare.welfare_op_log")
local biz_utils  = require("modules.business_common.business_utils")

local error_code = welfare_code.error_code

local FOO = {
    _VERSION = "0.12.1", -- 版本号
}
--初始化配置
FOO.init_worker_by_lua_file = function()
    lua_config.OnInit()
end

--初始化
FOO.init = function()
    gg.init()
end

--收尾工作
FOO.final = function()
    gg.final()
end

--入口函数
FOO.enter = function()
    ngx.ctx.m_params = ngx.req.get_uri_args();
    
    if not FOO.parse_body_data() then
        return
    end

    ngx.ctx.log          = tonumber(ngx.ctx.m_params.log) or 0
    ngx.ctx.m_params.uin = tonumber(ngx.ctx.m_params.uin) or 0
    ngx.ctx.xff          = biz_utils.real_client_ip() or "nil";

	local action_     = nil;
	local commands_   = nil;

	if	ngx.ctx.m_params.act then
		action_      = ngx.ctx.m_params.act;     --外网客户端act
		commands_    = acts_func;

	elseif ngx.ctx.m_params.cmd then
		action_      = ngx.ctx.m_params.cmd;     --内网命令cmd
		commands_    = cmds_func;
	else
		log.debug("params error0.");
		return;
	end

    --开始条件判定
    for _, condition_check in ipairs(auth_checker.condition_list or {}) do
        local ret, reason = condition_check()
        if not ret then
            gg.return_table({ret = reason})
            return
        end
    end

    if not commands_[ action_ ] then
        log.debug( "command ignore." .. (action_ or "empty") );
        return
    end

    FOO.init()

    local ret, data = commands_[ action_ ]()
    if not ret then
        gg.return_table({ret = error_code.ERROR_OP})
        op_log.record("welfare_error_op", 'not_ret', {action_ = action_})
    else
        gg.return_table({ret = ret, data = data})
        if ret ~= error_code.COMMON_SUCCESS then
            op_log.record("welfare_error_op", 'ret_error', {action_ = action_, ret=ret})
        end
    end

    FOO.final()
end


FOO.parse_body_data = function()
    if not auth_checker.ns_http_sec.decodeS7Url() then
        return false
    end

    if "POST" == ngx.var.request_method then

        ngx.req.read_body()
        local t = cjson.decode(ngx.req.get_body_data())

        if t == nil then
            t = table.loadstring(ngx.req.get_body_data()) --此时尝试用这种格式
        end
        log.debug("recv:" .. ngx.req.get_body_data())
        if not t then
            log.error("decode body failed.")
            return false
        end
        -- 合并参数
        for k, v in pairs(t) do
            ngx.ctx.m_params[k] = v
        end
    end

    return true
end


return FOO

